package com.xu.controller;

import com.xu.mapper.Vouchers_userMapper;
import com.xu.pojo.*;
import com.xu.service.OrderService;
import com.xu.service.ProductService;
import com.xu.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;


    @Autowired
    private StockService stockService;

    @Autowired
    private Vouchers_userMapper vouchers_userMapper;

    @RequestMapping("/queryAllOrder")
    public String queryAllOrder(Model model,HttpServletRequest request)
    {
        List<Order> order = orderService.queryAllOrder();
        model.addAttribute("order", order);
//        request.setAttribute("order", order);
        System.out.println(order);
        return "admin/order/order";
    }
    @RequestMapping("/queryOrderById")
    public String queryOrderById(int id,Model model)
    {
        Order order = orderService.queryOrderById(id);
        model.addAttribute("all", order);
        return "order";
    }
    @RequestMapping("/setOrder")
    public String jumpOrder(HttpSession httpSession, String id, Model model, Integer num)
    {
        Integer paraInt = Integer.parseInt(id.trim());
        //获取uid
        Integer uid = Integer.parseInt(httpSession.getAttribute("uid").toString());
        Product product = productService.queryProductById(paraInt);

        Vouchers_user voucher = new Vouchers_user();
        voucher.setVid(5);
        voucher.setUid(uid);
        Vouchers_user vouchers_user = vouchers_userMapper.queryVouchersByVidUid(voucher);

        Stock stock = stockService.queryStockById(paraInt);

        if (httpSession.getAttribute("username")==null)
        {
            model.addAttribute("msg3", "未登录，请登录或注册后下单");
            return "login";
        }
        else
        {
            //优惠券信息
            if (product.getPprice()>=20)
            {
                System.out.println("pid-----------------------》"+id);
                System.out.println(vouchers_user);
                if (vouchers_user!=null)
                {
                    System.out.println(stock);
                    model.addAttribute("msg", id);

                    double pprice = product.getPprice();
                    BigDecimal bd = new BigDecimal(pprice) ;
                    double test1 = bd.setScale(1, BigDecimal.ROUND_DOWN).doubleValue()-10;
                    product.setPprice(test1);

                    model.addAttribute("msg1", product);
                    model.addAttribute("msg2", stock);
                    model.addAttribute("msg3", num);
                    model.addAttribute("msg4", vouchers_user);
                    return "user/product/product-confirm";
                }
                else
                {
                    System.out.println(stock);
                    model.addAttribute("msg", id);
                    model.addAttribute("msg1", product);
                    model.addAttribute("msg2", stock);
                    model.addAttribute("msg3", num);
                    return "user/product/product-confirm";
                }
            }
            //
            else
            {

                if (vouchers_user!=null)
                {
                    System.out.println(stock);
                    model.addAttribute("msg", id);
                    double pprice = product.getPprice();
                    BigDecimal bd = new BigDecimal(pprice) ;
                    double test1 = bd.setScale(1, BigDecimal.ROUND_DOWN).doubleValue()-5;
                    product.setPprice(test1);

                    model.addAttribute("msg1", product);
                    model.addAttribute("msg2", stock);
                    model.addAttribute("msg3", num);
                    model.addAttribute("msg4", vouchers_user);
                    return "user/product/product-confirm";
                }
                else
                {
                    System.out.println("pid-----------------------》"+id);
                    System.out.println(stock);
                    model.addAttribute("msg", id);
                    model.addAttribute("msg1", product);
                    model.addAttribute("msg2", stock);
                    model.addAttribute("msg3", num);
                    return "user/product/product-confirm";
                }
            }
        }
    }
    @RequestMapping("/addOrder")
    public String addOrder(Order order, Integer num, String id, Model model, Stock stock)
    {
        Integer paraInt = Integer.parseInt(id.trim());
        if (num>(stockService.queryStockById(paraInt)).getNum())
        {
            model.addAttribute("msg3", "超出库存数，请重新输入");
            return "dashboard-user";
        }
        else
        {
            System.out.println(order);
            Stock stock1 = stockService.queryStockById(paraInt);
            orderService.addOrder(order);
            stock.setId(paraInt);
            stock.setNum((stock1.getNum())-num);
            stockService.updStock(stock);
            model.addAttribute("msg4", "下单成功");
            return "user/order/order-finish";
        }

    }
    Order order1 = new Order();
    @RequestMapping("/toUpdOrder/{id}")
    public String toUpdOrder(@PathVariable("id") int id, Model model)
    {
        System.out.println(id);
        order1 = orderService.queryOrderById(id);
        System.out.println("all--------------------------->"+order1);
        model.addAttribute("msg5", order1);
        return "admin/order/order-upd";
    }
    @RequestMapping("/updOrder")
    public String updOrder(int id,int num,int pid,Order order)
    {
        System.out.println("id------------->"+id);
        System.out.println("num------------->"+num);
        Order order2 = orderService.queryOrderById(id);
        System.out.println("order2.getNum------------->"+order2.getNum());
        int nowStock = (stockService.queryStockById(pid)).getNum();
        System.out.println("nowStock------------->"+nowStock);
        int chgStock = order2.getNum()-num;
        System.out.println("chgStock------------->"+chgStock);
        Stock stock = new Stock();
        stock.setId(pid);
        stock.setNum(nowStock+chgStock);
        stockService.updStock(stock);
        Order order3 = new Order();
        order3.setId(id);
        order3.setNum(num);
        orderService.updOrder(order3);
        return "redirect:/queryAllOrder";
    }
    @RequestMapping("/delOrder/{id}/{num}/{pid}")
    public String delOrder(@PathVariable("id") int id,@PathVariable("num")int num,@PathVariable("pid")int pid,Order order)
    {
        System.out.println("id------------->"+id);
        System.out.println("num------------->"+num);
        Order order2 = orderService.queryOrderById(id);
        System.out.println("order2.getNum------------->"+order2.getNum());
        int nowStock = (stockService.queryStockById(pid)).getNum();
        System.out.println("nowStock------------->"+nowStock);
        Stock stock = new Stock();
        stock.setId(pid);
        stock.setNum(nowStock+num);
        stockService.updStock(stock);
        orderService.delOrder(id);
        return "redirect:/queryAllOrder";
    }

    @ResponseBody
    @RequestMapping("/testQueryAllOrder")
    public List<Order> test()
    {
        return orderService.queryAllOrder();
    }
}
